package com.afs.electronicforensics.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.os.Environment;

import com.afs.electronicforensics.entity.FileInfo;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileUtil {

    //获得路径
    public static String getPath(Activity activity) {
        File dir = null;
        boolean state = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (state) {
            if (Build.VERSION.SDK_INT >= 29) {
                //Android10之后
                dir = activity.getExternalFilesDir(null);
            } else {
                dir = Environment.getExternalStorageDirectory();
            }
        } else {
            dir = Environment.getRootDirectory();
        }
        return dir.toString();
    }
    //设置输出文件夹路径
    public static String setOutPutPath(Activity activity,String name){
        String outputPath = getPath(activity) + name;
        File file = new File(outputPath);
        if(!file.exists()){
            file.mkdirs();// 创建以此File对象为名（path）的文件夹
        }
        return outputPath;
    }

    //获得当前时间
    private static String currentDate(){
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyyMMddHHmmss");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        return sdf.format(date);
    }

    //设置文件路径
    public static String setPath(int choice){
        switch (choice){
            case 1:
                //TODO 设置图片路径名称
                return currentDate() + ".jpg";
            case 2:
                //TODO 设置视频路径名称
                return currentDate() + ".mp4";
            case 3:
                //TODO 设置音频路径名称
                return currentDate() + ".mp3";
            case 4:
                //TODO 设置头像路径
                return Constants.USER_ROOT.getUid() + "_avatar.jpg";
            default:
                return null;
        }
    }

    //删除文件
    public static boolean deleteFile(String filePath) {
        File file = new File(filePath);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            file.delete();
            return true;
        } else {
            return false;
        }
    }

    //获取网络视频缩略图
    public static Bitmap getNetVideoBitmap(String videoUrl) {
        Bitmap bitmap = null;

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            //根据url获取缩略图
            retriever.setDataSource(videoUrl, new HashMap());
            //获得第一帧图片
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }
        return bitmap;
    }

    //模糊查询
    public static ArrayList<FileInfo> search(String name, ArrayList<FileInfo> list){
        ArrayList<FileInfo> results = new ArrayList();
        Pattern pattern = Pattern.compile(name);
        for(int i=0; i < list.size(); i++){
            Matcher matcher = pattern.matcher((list.get(i)).getTitle());
            if(matcher.find()){
                results.add(list.get(i));
            }
        }
        return results;
    }




}
